package com.ohxwj.modules.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.ohxwj.common.exception.RRException;
import com.ohxwj.common.utils.PropertiesUtils;
import com.ohxwj.common.utils.PushToAndroid;
import com.ohxwj.common.utils.R;
import org.apache.commons.configuration.Configuration;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/web")
public class UploadTestController extends AbstractAction {

    /**
     * 上传文件
     */
    @RequestMapping("/uploadImage")
    public R upload(@RequestParam("image") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        Configuration config = PropertiesUtils.getConfig();

        String filePath = config.getString("fileRoute" ) + "image/";
        File fileAdress =new File(filePath);
        //如果文件夹不存在则创建
        if  (!fileAdress .exists()  && !fileAdress .isDirectory())
        {
            fileAdress.mkdirs();
        }

        String fileName = UUID.randomUUID().toString() + ".jpg";
        filePath = filePath+ fileName;
        // 转存文件
        file.transferTo(new File(filePath));


        //真实路径存到数据库
        String realPath = "image/" + fileName;

        R r = R.ok();
        r.put("data", realPath);
        r.put("link", config.getString("fileAddress") + fileName);

        return r;
    }

    /**
     * 上传文件
     */
    @RequestMapping("/uploadBase64Image")
    public R uploadBase64Image(@RequestParam("base64") String base64) throws Exception {
        Configuration config = PropertiesUtils.getConfig();
//        String base64 = getStringParamFromRequest(request, "base64");
        String filePath = config.getString("fileRoute" ) + "image/";
        File fileAdress =new File(filePath);
        //如果文件夹不存在则创建
        if  (!fileAdress .exists()  && !fileAdress .isDirectory())
        {
            fileAdress.mkdirs();
        }

        String fileName = UUID.randomUUID().toString() + ".jpg";
        filePath = filePath+ fileName;
        // 转存文件
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b = decoder.decodeBuffer(base64);
        for (int i = 0; i < b.length; ++i) {
            if (b[i] < 0) {
                b[i] += 256;
            }
        }
        OutputStream out = new FileOutputStream(filePath);
        out.write(b);
        out.flush();
        out.close();

        //真实路径存到数据库
        String realPath = "image/" + fileName;

        R r = R.ok();
        r.put("data", realPath);
        r.put("link", config.getString("fileAddress") + fileName);

        return r;
    }
}
